using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class JurnalDTO : INotifyPropertyChanged
    {
        private String _Description;
        private ObservableCollection<JurnalDetailDTO> _JurnalDetails;
        private Guid _JurnalId;
        private String _NoVoucher;


        private DateTime _TglJurnal;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;
        private string _Departemen;
        private int _No;
        private CostCenterDTO _CostCenter;
        private string _TipeJurnal;
        private string _ReffNumber;
        private decimal _Balance;
        private decimal _TotalDebit;
        private decimal _TotalKredit;


        public JurnalDTO()
        {
            TglJurnal = DateTime.Now.Date;
            JurnalDetails = new ObservableCollection<JurnalDetailDTO>();
        }

        [DataMember]
        public DateTime TglJurnal
        {
            get { return _TglJurnal; }
            set
            {
                _TglJurnal = value;
                RaisePropertyChanged("TglJurnal");
            }
        }


        [DataMember]
        public String NoVoucher
        {
            get { return _NoVoucher; }
            set
            {
                _NoVoucher = value;
                RaisePropertyChanged("NoVoucher");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public ObservableCollection<JurnalDetailDTO> JurnalDetails
        {
            get { return _JurnalDetails; }
            set
            {
                if (ReferenceEquals(JurnalDetails, value) || value == null) return;
                _JurnalDetails = value;
                RaisePropertyChanged("JurnalDetails");
                RefreshNumber();
            }
        }

        [DataMember]
        public String Departemen
        {
            get { return _Departemen; }
            set
            {
                _Departemen = value;
                RaisePropertyChanged("Departemen");
            }
        }


      


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }



        [DataMember]
        public String TipeJurnal
        {
            get { return _TipeJurnal; }
            set
            {
                _TipeJurnal = value;
                RaisePropertyChanged("TipeJurnal");
            }
        }

        [DataMember]
        public String ReffNumber
        {
            get { return _ReffNumber; }
            set
            {
                _ReffNumber = value;
                RaisePropertyChanged("ReffNumber");
            }
        }

        public Decimal TotalDebit
        {
            get { return _TotalDebit; }
            set
            {
                _TotalDebit = value;
                RaisePropertyChanged("TotalDebit");
            }
        }

        public Decimal TotalKredit
        {
            get { return _TotalKredit; }
            set
            {
                _TotalKredit = value;
                RaisePropertyChanged("TotalKredit");
            }
        }

        public Decimal Balance
        {
            get { return _Balance; }
            set
            {
                _Balance = value;
                RaisePropertyChanged("Balance");
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        private void RefreshNumber()
        {
            JurnalDetails.CollectionChanged += JurnalDetails_CollectionChanged;
            int number = 0;
            foreach (var jd in JurnalDetails)
            {
                number += 1;
                jd.No = number;
            }
        }

        void JurnalDetails_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (var jd in JurnalDetails)
            {
                number += 1;
                jd.No = number;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}